﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using AForge.Video;
using AForge.Video.DirectShow;

namespace mycapturer
{
    public class CaptureHelper
    {
        public CaptureHelper(DeviceInfo deviceInfo, bool isDynamic)
        {
            _deviceInfo = deviceInfo;
            _isDynamic = isDynamic;
            _captureAForge = new VideoCaptureDevice(deviceInfo.MonikerString);
            _captureAForge.NewFrame += new NewFrameEventHandler(captureAForge_NewFrame);
        }

        private DeviceInfo _deviceInfo;
        private VideoCaptureDevice _captureAForge = null;    //AForge视频捕获对象
        private bool _isDynamic;

        public static DeviceInfo[] GetAllCaptures()
        {
            List<DeviceInfo> deviceInfos = new List<DeviceInfo>();
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count > 0)
            {
                int idx = 0;
                foreach (FilterInfo device in videoDevices)
                {
                    deviceInfos.Add(new DeviceInfo(device.Name, device.MonikerString, idx, FilterCategory.VideoInputDevice));
                    idx++;
                }
            }

            return deviceInfos.ToArray();
        }

        public void StartCapture()
        {
            if (!_captureAForge.IsRunning)
            {
                _captureAForge.Start();
            }
        }

        public void StopCapture()
        {
            if (_captureAForge.IsRunning)
            {
                _captureAForge.SignalToStop();
            }
        }

        public void CapturePicture(int pictureCount, int interval)
        {
            
        }

        private void captureAForge_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            //eventArgs.Frame.Save(Path.Combine("c:\\temp", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_fff") + ".bmp"));
            
        }

       
    }

    //设备信息
    public struct DeviceInfo
    {
        public string Name;
        public string MonikerString;
        public int Index;
        private Guid Category;

        public DeviceInfo(string name, string monikerString, int index) :
            this(name, monikerString, index, Guid.Empty)
        {
        }

        public DeviceInfo(string name, string monikerString, int index, Guid category)
        {
            Name = name;
            MonikerString = monikerString;
            Index = index;
            Category = category;
        }

        public override string ToString()
        {
            return Name;
        }
    }

}
